﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Repository;
using PersonalHealth.Data.Model;
using Core.UnitOfWork;
using PersonalHealth.Data.Data;

namespace PersonalHealth.Data.Repository
{
    public class DoctorRepository : Repository<Doctor>, IDoctorRepository
    {
        public DoctorRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        
        }

        public Doctor GetDoctorDetail(int id)
        {
            return DbSet.Include("Sex").Include("Department").SingleOrDefault(c => c.Id == id);
        }

        public IList<Doctor> GetDoctorDetailList<Tkey>(Func<Doctor, bool> expression,
          Func<Doctor, Tkey>[] orderby, bool[] isDescending, int pageIndex, int pageCount, out int pages, out int records)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");
            if (orderby == null)
                throw new ArgumentNullException("orderby");

            if (orderby.Length != isDescending.Length)
                throw new ArgumentException("orderby count not equal isDescending count!");

            var q = DbSet.Include("Sex").Include("Department").Where(expression);
            records = q.Count();
            pages = (records + pageCount - 1) / pageCount;

            var orderQ = isDescending[0] ? q.OrderByDescending(orderby[0]) : q.OrderBy(orderby[0]);
            for (int i = 1; i < orderby.Length; i++)
            {
                orderQ = isDescending[i] ? orderQ.ThenByDescending(orderby[i]) : orderQ.ThenBy(orderby[i]);
            }
            return orderQ.Skip((pageIndex - 1) * pageCount).Take(pageCount).ToList();
        }
    }
}
